﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cadbom
{
    public partial class QueryMaterialForm : Form
    {
        private System.Windows.Forms.Timer timer;
        private int searchDelay = 500; // 设置延时，单位毫秒
        NetApi netApi = new NetApi();
        List<BasMaterial> items = new List<BasMaterial>();
        private System.Data.DataTable dtBand = null;
        private Action<BasMaterial> callback;
        public QueryMaterialForm(Action<BasMaterial> callback)
        {
            this.callback = callback;
            InitializeComponent();

            timer = new System.Windows.Forms.Timer();
            timer.Interval = searchDelay;
            timer.Tick += new EventHandler(Timer_Tick);
            //设置复选框列的属性
            //DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            //checkBoxColumn.HeaderText = "选择";
            //checkBoxColumn.Name = "IsSelected";
            //checkBoxColumn.DataPropertyName = "IsSelected";
            //dataGridView1.Columns.Add(checkBoxColumn);
        }

        private void QueryMaterialForm_Load(object sender, EventArgs e)
        {

        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            // 在这里编写查找逻辑
            PerformSearch(textBox1.Text);
        }
        private void PerformSearch(string searchText)
        {
            // 执行查找操作
            if (searchText != null && searchText != "")
            {
                netApi.getMaterialData(searchText, new Action<ResponResultA<BasMaterial>>((rs) =>
                {
                    BindingList<BasMaterial> products = rs.result.records;
                    List<BasMaterial> items = products.ToList();
                    dtBand = new System.Data.DataTable();
                    this.items = items;
                    dtBand.Columns.Add("code");
                    dtBand.Columns.Add("nameDesc");
                    dtBand.Columns.Add("model");
                    dtBand.Columns.Add("description");
                    dtBand.Columns.Add("version");
                    dtBand.Columns.Add("typeName");
                    dtBand.Columns.Add("unitId_dictText");
                    dtBand.Columns.Add("id");
                    foreach (BasMaterial mpb in items)
                    {
                        DataRow dr = dtBand.NewRow();
                        dr["code"] = mpb.code;
                        dr["nameDesc"] = mpb.nameDesc;
                        dr["model"] = mpb.model;
                        dr["description"] = mpb.description;
                        dr["version"] = mpb.materialVer;
                        dr["typeName"] = mpb.typeName;
                        dr["unitId_dictText"] = mpb.unitId_dictText;
                        dr["id"] = mpb.id;
                        dtBand.Rows.Add(dr);
                    }
                    this.dataGridView1.DataSource = dtBand;
                    dataGridView1.Columns["code"].HeaderText = "物料编码";
                    dataGridView1.Columns["nameDesc"].HeaderText = "物料名称";
                    dataGridView1.Columns["model"].HeaderText = "物料规格";
                    dataGridView1.Columns["description"].HeaderText = "物料描述";
                    dataGridView1.Columns["version"].HeaderText = "版本";
                    dataGridView1.Columns["typeName"].HeaderText = "物料类型";
                    dataGridView1.Columns["unitId_dictText"].HeaderText = "单位";
                    this.dataGridView1.Columns["id"].Visible = false;
                    foreach (DataGridViewColumn item in dataGridView1.Columns) item.SortMode = DataGridViewColumnSortMode.NotSortable;

                }));
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Start();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            timer.Stop();
            // 在这里编写查找逻辑
            PerformSearch(textBox1.Text);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var indexRow = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Index;
            var indexCol = ((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex;
            if (this.dataGridView1.CurrentCell.OwningColumn.DataPropertyName == "processId")
            {
            }
            //MessageBox.Show(this.items[indexRow].code);
            callback.Invoke(this.items[indexRow]);
            //this.Close();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using
           (SolidBrush b =
           new
           SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {

                string
                linenum = e.RowIndex.ToString();
                int
                linen = 0;
                linen = Convert.ToInt32(linenum) + 1;
                string
                line = linen.ToString();
                e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 5);
                SolidBrush B =
                new
                SolidBrush(Color.Red);
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                // 获取更改的单元格值
                var changedCellValue = dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value;

            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
